package com.bwie.user.service.impl;

import com.bwie.common.utils.R;
import com.bwie.user.conf.JwtUtil;
import com.bwie.user.conf.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.user.dao.UserDao;
import com.bwie.user.entity.UserEntity;
import com.bwie.user.service.UserService;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 缓存用户表第一页
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        String page1 = (String) params.get("page");
        IPage<UserEntity> userList = (IPage<UserEntity>) redisTemplate.boundHashOps("user").get("list");

        //缓存中没有并且key为空并且是第一页
        if (userList==null&&Integer.parseInt(page1)==1&&StringUtils.isEmpty(key)){
            //从数据库查找，并缓存
            IPage<UserEntity> page = this.page(
                    new Query<UserEntity>().getPage(params),
                    new QueryWrapper<>()
            );
            System.out.println("查询数据库并缓存第一页");
            redisTemplate.boundHashOps("user").put("list",page);
            return new PageUtils(page);
        }else if (userList!=null&&Integer.parseInt(page1)==1&&StringUtils.isEmpty(key)){
            System.out.println("缓存命中");
            return new PageUtils(userList);
        }else {
            System.out.println("直接去数据库查userList");
            //如果key不为空 去数据库查找
            IPage<UserEntity> page = this.page(
                    new Query<UserEntity>().getPage(params),
                    new QueryWrapper<UserEntity>().eq("uid",key)
            );

            return new PageUtils(page);
        }

    }

    /**
     * 登录+密码解密
     * @param user
     * @return
     */
    @Override
    public R login(UserEntity user) {
        UserEntity userEntity=baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("nickname",user.getNickname()));
        if (userEntity!=null&&userEntity.getPassword().equals(MD5Util.md5Code(user.getPassword(),2))){
            String token = jwtUtil.createJWT(userEntity.getUid() + "", userEntity.getNickname(), "user");
            //修改用户表中的最后登录时间，还有登录次数
            userEntity.setLastLoginData(new Date());
            userEntity.setLoginCount(userEntity.getLoginCount()+1);
            baseMapper.updateById(userEntity);
            return R.ok().setData(token).put("user",userEntity);
        }
        return R.error("用户名或密码错误");
    }
}